package com.geuso.xingdutch.sentances;

import java.util.ArrayList;

import com.geuso.xingdutch.R;
import com.geuso.xingdutch.util.SoundUtil;
import com.geuso.xingdutch.words.Word;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SentancesListAdapter extends BaseExpandableListAdapter{

	private LayoutInflater inflater;
	private Context context;
	private ArrayList<Sentance> sentances;
	
	private static int max_screen_width;
	private static DisplayMetrics metrics;
	private static int sound_img_width;
	
	public SentancesListAdapter(Context context, ArrayList<Sentance> sentances){
		super();
		this.context = context;
		this.sentances = sentances;
		this.inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		
		Point p = new Point();
		((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(p);
		max_screen_width = p.x;
		
		metrics = context.getResources().getDisplayMetrics();
		Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_lock_silent_mode_off);
		sound_img_width = bmp.getWidth();
		
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return sentances.get(groupPosition).getSentance_part().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

		final Word w = (Word) getChild(groupPosition, childPosition);

		if (w == null){
			return null;
		}
		
		View v = inflater.inflate(R.layout.sentance_list_item_detail, null);
		
		TextView dutch_word = (TextView) v.findViewById(R.id.sen_d_dutch);
		TextView dutch_pronounce = (TextView) v.findViewById(R.id.sen_d_dutch_pronounce);
		TextView english = (TextView) v.findViewById(R.id.sen_d_en);
		TextView chinese = (TextView) v.findViewById(R.id.sen_d_cn);
		ImageView sound_file = (ImageView) v.findViewById(R.id.sen_d_play_sound) ;
		
		if (dutch_word != null)		dutch_word.setText(w.getDutch());
		if (dutch_pronounce != null)	dutch_pronounce.setText(w.getDutch_pronounciation());
		if (english != null) 	english.setText(w.getEnglish());
		if (chinese != null) 	chinese.setText(w.getChinese());
		
		if (sound_file != null){
			if ( w.getSound_file() > 0) {
				sound_file.setVisibility(View.VISIBLE);
				SoundUtil.setOnClickPlaySound(sound_file, w.getSound_file());
			}
			else 	sound_file.setVisibility(View.INVISIBLE);
		}
		
		return v;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return sentances.get(groupPosition).getSentance_part().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return sentances.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return sentances.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		
		final Sentance s = (Sentance) getGroup(groupPosition);
		if (s == null)	return null;
		
		
		View v = convertView;
			
		if (v == null)
			v = inflater.inflate(R.layout.sentance_list_item, null);
		
		
		TextView dutch_word = (TextView) v.findViewById(R.id.sen_dutch);
		TextView dutch_pronounce = (TextView) v.findViewById(R.id.sen_dutch_pronounce);
		TextView english = (TextView) v.findViewById(R.id.sen_english);
		TextView chinese = (TextView) v.findViewById(R.id.sen_chinese);
		ImageView sound_file = (ImageView) v.findViewById(R.id.sen_play_sound) ;
		
		if (dutch_word != null){	
			dutch_word.setText(s.getDutch());
			fitTextViewOnScreen(dutch_word, max_screen_width - (sound_img_width + 20 + 24));
		}
		if (dutch_pronounce != null)	dutch_pronounce.setText(s.getDutch_pronounciation());
		if (english != null) 	english.setText(s.getEnglish());
		if (chinese != null) 	chinese.setText(s.getChinese());
		
		if (sound_file != null){
			if ( s.getSound_file() > 0) {
				sound_file.setVisibility(View.VISIBLE);
				SoundUtil.setOnClickPlaySound(sound_file, s.getSound_file());
			}
			else 	sound_file.setVisibility(View.INVISIBLE);
		}
		
		//v.setBackgroundResource(R.drawable.list_selector);
		return v;
	}


	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	
	
	
	private int calcTextViewWidth(TextView v){
		Paint paint = new Paint();
		Rect bounds = new Rect();

		paint.setTypeface(Typeface.DEFAULT);
		paint.setTextSize(v.getTextSize());

		paint.getTextBounds(v.getText().toString(), 0, v.getText().length(), bounds);

		return  bounds.width();
	}
	
	private void fitTextViewOnScreen(TextView v, int max_width){
		if (v.getText().toString().length() < 24){
			return;
		}
			
		while(true){
			int width = calcTextViewWidth(v);
			if (width > max_width){
				v.setTextSize(TypedValue.COMPLEX_UNIT_SP, pixelsToSp(v.getTextSize())-1);
				if (pixelsToSp(v.getTextSize()) <= 10){
					break;
				}
			}
			else {
				break;
			}
		}
	}
	
	public float pixelsToSp(float px) {
	    float scaledDensity = metrics.scaledDensity;
	    return px/scaledDensity;
	}
	
}
